package cn.itcast.order.web;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/quartzStart")
public class QuartzRestartController {

    @Autowired
    private Scheduler scheduler;

    @PostMapping("/resumeTrigger")
    public String resumeTrigger(@RequestParam String triggerName, @RequestParam(required = false, defaultValue = "defaultGroup") String triggerGroup) {
        try {
                TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
            // 检查触发器是否存在
            if (scheduler.checkExists(triggerKey)) {
                // 检查触发器状态，如果是PAUSED则恢复
                Trigger.TriggerState state = scheduler.getTriggerState(triggerKey);
                if (state == Trigger.TriggerState.PAUSED) {
                    scheduler.resumeTrigger(triggerKey);
                    return "Trigger " + triggerName + " resumed successfully.";
                } else {
                    return "Trigger " + triggerName + " is not paused. Current state: " + state;
                }
            } else {
                return "Trigger " + triggerName + " does not exist.";
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
            return "Error resuming trigger: " + e.getMessage();
        }
    }
}